﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
using MMOEng;
using MMOShared.Packets;

namespace MMOConnector
{
    public partial class serverListForm : Form
    {
        private TcpClient _client;
        private NetworkStream _stream;
        private BinaryFormatter _bf;
        private GameConfiguration gameConfig;
        private VersionsAvailable va;
        public serverListForm(TcpClient client)
        {
            //This is a temp path, I need to figure a better way to track this
            gameConfig = GameConfiguration.Load("..\\..\\..\\MMOTest\\bin\\x86\\Debug\\game.conf");
            _bf = new BinaryFormatter();
            _client = client;
            _stream = client.GetStream();
            InitializeComponent();

            MMOShared.ServerList list = new MMOShared.ServerList();
            while (_client.Connected)
            {
                if (_stream.DataAvailable)
                {
                    list = _bf.Deserialize(_stream) as MMOShared.ServerList;
                    break;
                }
            }
            foreach (MMOShared.ServerEntry entr in list.Servers)
            {
                int u = 0;
                if (entr.IsAvailable) u = 1;
                this.serversLV.Items.Add(entr.ServerName,u);
            }
            
            //Request versions available
            RequestVersions(gameConfig);

            //Wait for version list
            VersionsAvailable versions = WaitForVersions();
            UpdateVersionLabels(versions,gameConfig);

            if (versions.LatestVersion > gameConfig.GameVersion)
            {
                updateB.Enabled = true;
            }
            else if (versions.LatestVersion == gameConfig.GameVersion)
            {
                playB.Enabled = true;
            }
            va = versions;
        }

        private void UpdateVersionLabels(VersionsAvailable versions, GameConfiguration gc)
        {
            this.currentVersionL.Text = "Game Version: " + gc.GameVersion;
            this.serverVersionL.Text = "Latest Version: " + versions.LatestVersion;
        }

        private VersionsAvailable WaitForVersions()
        {
            bool waitingForVersionList = true;
            VersionsAvailable versionList = null;
            while (waitingForVersionList)
            {
                if (_client.Connected && _stream.DataAvailable)
                {
                    object rec = _bf.Deserialize(_stream);
                    if (rec is VersionsAvailable)
                    {
                        versionList = (VersionsAvailable)rec;
                        waitingForVersionList = false;
                    }
                    else
                    {
                        MessageBox.Show("Error: Didn't receive available versions, received " + rec.ToString() + " instead.");
                    }
                }
            }
            return versionList;
        }

        private void RequestVersions(GameConfiguration gc)
        {
            UpdateRequest ur = new UpdateRequest(gc.GameVersion, UpdateRequest.Action.UPDATE_QUERY);
            _bf.Serialize(_stream, ur);
            _stream.Flush();
        }

        private void updateB_Click(object sender, EventArgs e)
        {
            FileDownloadForm fdf = new FileDownloadForm(gameConfig, _client, va);
            fdf.OnVersionUpdated += new FileDownloadForm.VersionUpdated(fdf_OnVersionUpdated);
            fdf.ShowDialog();
        }

        void fdf_OnVersionUpdated(ushort version)
        {
            MessageBox.Show("Update to version " + version + " completed successfully!");
            gameConfig.GameVersion = version;
            gameConfig.Save("..\\..\\..\\MMOTest\\bin\\x86\\Debug\\game.conf");
        }
    }
}
